package com.pearlsoft.webapp.sysmgr.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dao.PageResult;
import com.pearlsoft.webapp.core.dao.RecrodRowMapper;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.pearlsoft.webapp.sysmgr.role.service.RoleRowMapperHelper;
import com.pearlsoft.webapp.sysmgr.role.service.RoleService;

/**
 * 
 * @author YEHH
 *
 */
@QueryHandlerComponent(name = "SysRoleLoadHandler")
public class SysRoleLoadHandler extends AbsBaseQueryHandler {
	private final String generalFields=" ROLE_ID, ROLE_NAME, C_STATUS, C_REMARK ";
	private final RecrodRowMapper generalRowMapper = RoleRowMapperHelper.generalRowMapper();

	/**
	 * 角色查询
	 */
	public void handler(Query query, final DataSet ds) throws Exception {
		RoleService roleService = this.getBean("roleService", RoleService.class);
		String sql ="select "+generalFields+" from sys_role ";
		String roleName = null;
		Map<String,String> params = new HashMap<String,String>();
		
		if( query.get("ROLE_NAME") != null ){
			roleName =(String) query.get("ROLE_NAME");
		}
		if( !StringUtils.isBlank( roleName )){
			String where =" where ROLE_NAME like :ROLE_NAME";
			params.put("ROLE_NAME", "%"+roleName+"%");
			sql = sql + where;
		}
		if(query.getPageSize() > 0 ){
			
			PageResult pageResult  = roleService.getBaseSimpleJdbcDao().findByPage(sql, query.getStart(), query.getPageSize(), params, generalRowMapper);
			ds.setPageSize( query.getPageSize());
			ds.setTotalCount(pageResult.getTotalCount());
			ds.setRecords(pageResult.getRecords());
		}else{
			List<Record> list  = roleService.getBaseSimpleJdbcDao().query(sql, params, generalRowMapper);
			ds.setRecords(list);
		}
	}
	
	/***
	 * 根据角色Id得到角色recrod
	 */
	public void getEntity(Query query, DataSet ds) throws Exception {
		String roleId =(String) query.get("objId");
		RoleService roleService = this.getBean("roleService", RoleService.class);
		Record record = roleService.getById(roleId);
		ds.addRecrod(record);
	}
	
	/***
	 * 快速查询
	 */
	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		StringBuilder sqlbuffer = new StringBuilder();
		String searchValue = (String)query.get("searchValue") ;
		RoleService roleService = this.getBean("roleService", RoleService.class);
		String sql ="select "+generalFields+" from sys_role ";
		sqlbuffer.append(sql);
		
		if( !StringUtils.isBlank(searchValue) ){
			sqlbuffer.append(" where ");
			appendLike(sqlbuffer, "ROLE_NAME", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "C_REMARK", searchValue);
		}
		if(query.getPageSize() > 0 ){
			PageResult pageResult  = roleService.getBaseSimpleJdbcDao().findByPage(sqlbuffer.toString(), query.getStart(), query.getPageSize(), generalRowMapper);
			ds.setPageSize( query.getPageSize());
			ds.setTotalCount(pageResult.getTotalCount());
			ds.setRecords(pageResult.getRecords());
		}else{
			List<Record> list  = roleService.getBaseSimpleJdbcDao().query(sqlbuffer.toString(), generalRowMapper);
			ds.setRecords(list);
		}
	}

	/***
	 * 高级查询
	 */
	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		StringBuilder sqlbuffer = new StringBuilder();
		String sql ="select "+generalFields+" from sys_role ";
		sqlbuffer.append(sql);
		
		String roleName = (String)query.get("ROLE_NAME") ;
		String roleRemark = (String)query.get("C_REMARK") ;
		String roleStatus = (String)query.get("C_STATUS") ;
		RoleService roleService = this.getBean("roleService", RoleService.class);
		boolean hasWhere = false;
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "ROLE_NAME", roleName);
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "C_REMARK", roleRemark);
		hasWhere=appendAndWhereLike(sqlbuffer, hasWhere, "C_STATUS", roleStatus);
		
		if(query.getPageSize() > 0 ){
			PageResult pageResult  = roleService.getBaseSimpleJdbcDao().findByPage(sqlbuffer.toString(), query.getStart(), query.getPageSize(), generalRowMapper);
			ds.setPageSize( query.getPageSize());
			ds.setTotalCount(pageResult.getTotalCount());
			ds.setRecords(pageResult.getRecords());
		}else{
			List<Record> list  = roleService.getBaseSimpleJdbcDao().query(sqlbuffer.toString(), generalRowMapper);
			ds.setRecords(list);
		}
	}
	
}
